package com.youlian.manager.service;

import com.youlian.common.Constants;
import com.youlian.manager.api.UserAPI;
import com.youlian.manager.model.LoginResult;
import com.youlian.manager.model.UserParentsResult;
import com.youlian.manager.param.LoginParam;
import com.youlian.manager.param.RegisterParam;
import com.youlian.utils.JsonUtils;
import com.youlian.utils.Md5Util;


public class UserService extends BaseService{
	
	private UserAPI api;
	
	/**
	 * 用户登录
	 *
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public UserParentsResult login(String username, String password) throws Exception {

		UserParentsResult result = null;

		api = new UserAPI();
		LoginParam param = new LoginParam();

		param.setService(Constants.youlian_user_base_login);
		param.setLogin_id(username);
		param.setPassword(Md5Util.makeMd5Sum(new String(password).getBytes()));

		jsonData = api.login(param);
		System.out.println("jsonData:"+jsonData);
			result = (UserParentsResult) JsonUtils.parseJson2Obj(jsonData, UserParentsResult.class);
         
		return result;
	}
	/**
	 * 用户注册
	 *
	 * @param username
	 * @param password
	 * @return
	 * @throws Exception
	 */
	public UserParentsResult register(String username, String password) throws Exception {

		UserParentsResult result = null;

		api = new UserAPI();
		RegisterParam param = new RegisterParam();

		param.setService(Constants.youlian_user_base_register);

		param.setLogin_id(username);
		param.setPassword(Md5Util.makeMd5Sum(new String(password).getBytes()));

		jsonData = api.register(param);

			result = (UserParentsResult) JsonUtils.parseJson2Obj(jsonData, UserParentsResult.class);

		return result;
	}

}
